package com.example.mydemo103.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.mydemo103.R;
import com.example.mydemo103.bean.RankBook;

import java.util.List;

public class RankListViewAdapter extends ArrayAdapter<RankBook> {
    private int Id;
    public RankListViewAdapter(Context context, int textViewid, List<RankBook> objects){
        super(context,textViewid,objects);
        Id=textViewid;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        RankBook rankbook=getItem(position); //获取当前项的Fruit实例

        // 加个判断，以免ListView每次滚动时都要重新加载布局，以提高运行效率
        View view;
        ViewHolder viewHolder;
        if (convertView==null){

            // 避免ListView每次滚动时都要重新加载布局，以提高运行效率
            view= LayoutInflater.from(getContext()).inflate(Id,parent,false);

            // 避免每次调用getView()时都要重新获取控件实例
            viewHolder=new ViewHolder();
            viewHolder.rankbookid=view.findViewById(R.id.rankbookid);
            viewHolder.rankbookimg=view.findViewById(R.id.rankbookimg);

            viewHolder.rankbookname=view.findViewById(R.id.rankbookname);

            viewHolder.rankbooknum=view.findViewById(R.id.rankbooknum);


            // 将ViewHolder存储在View中（即将控件的实例存储在其中）
            view.setTag(viewHolder);
        } else{
            view=convertView;
            viewHolder=(ViewHolder) view.getTag();
        }

        // 获取控件实例，并调用set...方法使其显示出来
//        viewHolder.fruitImage.setImageResource(book.getBookname());
        viewHolder.rankbookname.setText(rankbook.getRankbookname());
        viewHolder.rankbookid.setText(rankbook.getRankbookid());
        viewHolder.rankbooknum.setText(rankbook.getRankbooknum());
        viewHolder.rankbookimg.setImageResource(rankbook.getRankbookimg());
//        viewHolder.rankbookname.setText("1");
//        viewHolder.rankbookid.setText("1");
//        viewHolder.rankbooknum.setText("1");
//        viewHolder.rankbookimg.setImageResource(R.drawable.book1);

        return view;
    }

    // 定义一个内部类，用于对控件的实例进行缓存
    class ViewHolder{

        TextView rankbookname;
        TextView rankbooknum;
        TextView rankbookid;
        ImageView rankbookimg;

    }
}

